package org.oschina.api.blog.order.controller;

import java.util.List;
import org.oschina.api.BaseController;
import org.oschina.api.blog.order.model.BlogOrder;
import org.oschina.api.blog.order.service.BlogOrderService;
import org.oschina.api.blog.order.vo.BlogOrderVo;
import org.oschina.bean.Result;
import org.oschina.config.Config;
import org.oschina.handler.validator.annotation.Validate;
import org.oschina.handler.validator.annotation.ValidateField;
import org.oschina.handler.validator.filter.TextOnlyFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(value=Config.API_PATH+"/blog/order")
public class BlogOrderController extends BaseController {

	@Autowired
	private BlogOrderService orderService;
	
	/**
	 * 返回
	 * @return
	 */
	
	@RequestMapping(value="/",method=RequestMethod.GET)
	public List<BlogOrderVo> findVo()
	{
		return orderService.findVo();
	}
	
	/**
	 * 创建
	 * @param order
	 * @return
	 */
	@Validate
	@RequestMapping(value="/create",method=RequestMethod.POST)
	public Result create
	(
		@Validate(validateField=
		{
			@ValidateField(name="name",filter={TextOnlyFilter.class}),
			@ValidateField(name="index",validateValue=true,min=1)
		}) BlogOrder order
	)
	{
		return orderService.create(order);
	}
	
	/**
	 * 更新
	 * @param order
	 * @return
	 */
	@Validate
	@RequestMapping(value="/update/{id}",method=RequestMethod.POST)
	public Result update
	(
		@Validate(validateField=
		{
			@ValidateField(name="id",validateValue=true,min=1),
			@ValidateField(name="name",filter={TextOnlyFilter.class}),
			@ValidateField(name="index",validateValue=true,min=1)
		}) BlogOrder order
	)
	{
		return orderService.update(order);
	}
}